﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Jzy.Workflow.Flow;
using Jzy.Workflow.Node;
using Jzy.Workflow.Line;
using System.Data;
using System.Text;
using Jzy.Workflow.Task;
using System.Xml.Linq;
using Jzy.Workflow.Snap;

namespace Jzy.Web.WorkFlow.Service
{
    /// <summary>
    /// Flow 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Flow : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
      
        /// <summary>
        /// 
        /// </summary>
        /// <param name="flowID"></param>
        /// <param name="taskID">如为空则从snap表中读取</param>
        /// <returns></returns>
        [WebMethod]
        public string GetWorkFlow(int flowID)
        {
            //FlowBO flow = new FlowBO();
            StringBuilder sb = new StringBuilder();

            sb.Append("<WorkFlow>");

                FlowNodeBO node = new FlowNodeBO();
                FlowLineBO line = new FlowLineBO();

                List<FlowNodeInfo> dtNode = node.GetList(flowID);

                List<FlowLineInfo> dtLine = line.GetList(flowID);


                sb.Append("<Nodes>");
                foreach (FlowNodeInfo dr in dtNode)
                {
                    sb.Append("<Node>");
                    sb.Append("<ID>" + dr.ID + "</ID>");
                    sb.Append("<Name>" + dr.Name + "</Name>");
                    sb.Append("<TypeID>" + dr.TypeID + "</TypeID>");
                    sb.Append("<Left>" + dr.Left + "</Left>");
                    sb.Append("<Top>" + dr.Top + "</Top>");
                    sb.Append("<Width>" + dr.Width + "</Width>");
                    sb.Append("<Height>" + dr.Height + "</Height>");
                    sb.Append("</Node>");
                }
                sb.Append("</Nodes>");

                sb.Append("<Lines>");
                foreach (FlowLineInfo dr in dtLine)
                {
                    sb.Append("<Line>");
                    sb.Append("<ID>" + dr.ID + "</ID>");
                    sb.Append("<Name>" + dr.Name + "</Name>");
                    sb.Append("<TypeID>" + dr.TypeID + "</TypeID>");
                    sb.Append("<FromNodeID>" + dr.FromNodeID + "</FromNodeID>");
                    sb.Append("<ToNodeID>" + dr.ToNodeID + "</ToNodeID>");
                    sb.Append("</Line>");
                }
                sb.Append("</Lines>");
           
            sb.Append("</WorkFlow>");

            return sb.ToString();
        }

        private string GetWorkFlow(int flowID, string taskID)
        {
            if (string.IsNullOrEmpty(taskID))
            {
                return GetWorkFlow(flowID);
            }
            else
            {
                StringBuilder sb = new StringBuilder();

                sb.Append("<WorkFlow>");

                FlowSnapInfo snapInfo = (new FlowTaskBO()).GetInfo(taskID).FormInfo.SnapInfo;
                sb.Append(snapInfo.Nodes);
                sb.Append(snapInfo.Lines);
                sb.Append("</WorkFlow>");

                return sb.ToString();
            }
        }

        [WebMethod]
        public string GetWorkFlowWithApprovedNode(int flowID, string taskID)
        {
            string str = GetWorkFlow(flowID, taskID);

            if (!string.IsNullOrEmpty(taskID))
            {
                XElement flow = XElement.Parse(str);

                XElement approvedNode = new XElement("ApprovedNodes");

                FlowTaskBO task = new FlowTaskBO();
                IEnumerable<FlowTaskInfo> list = task.GetApprovedTask(taskID);

                //List<FlowTaskInfo> list = new List<FlowTaskInfo>();//NodeID有可能重复，去重取最新的
                //foreach (FlowTaskInfo info in lstTask)
                //{
                //    if (!list.Select(T => T.NodeID).Contains(info.NodeID))
                //    {
                //        list.Add(info);
                //    }
                //}               

                foreach (FlowTaskInfo info in list)
                {
                    XElement node = new XElement("Node");

                    XElement ele = new XElement("TaskID");
                    ele.Value = info.ID.ToString();
                    node.Add(ele);

                    ele = new XElement("NodeID");
                    ele.Value = info.ReceiveNodeID.ToString();
                    node.Add(ele);

                    ele = new XElement("State");
                    ele.Value=info.State.ToString();
                    node.Add(ele);

                    ele = new XElement("SendTime");
                    ele.Value = info.SendTime.ToString();
                    node.Add(ele);

                    approvedNode.Add(node);
                }

                flow.Add(approvedNode);

                str = flow.ToString();
            }

            return str;
            
        }


        [WebMethod]
        public bool SaveNodePosition(int id, double left, double top)
        {
            bool flag = true;
            try
            {
                FlowNodeBO node = new FlowNodeBO();
                node.SavePosition(id, left, top);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }

        [WebMethod]
        public void DeleteNode(object id)
        {
            FlowNodeBO node = new FlowNodeBO();
            node.Delete(Convert.ToInt32(id));
        }

        [WebMethod]
        public void DeleteLine(object id)
        {
            FlowLineBO line = new FlowLineBO();
            line.Delete((int)id);
        }

        /// <summary>
        /// 获取被审批的节点
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        //[WebMethod]
        //public List<int> GetApprovedNodes(int taskID)
        //{
        //    FlowTaskBO task = new FlowTaskBO();


        //    return task.GetApprovedTask(taskID);
            
        //}

        //[WebMethod]
        //public int GetNodeID(int taskID)
        //{
        //    FlowTaskBO task = new FlowTaskBO();

        //    return task.GetNodeID(taskID);
        //}
    }
}
